    //
//  AttendSearchResultsViewController.m
//  BVCMS
//
//  Created by Allen Thomas on 11/8/10.
//  Copyright 2010 __MyCompanyName__. All rights reserved.
//

#import "AttendSearchResultsViewController.h"
#import "MemberCheckinViewController.h"
#import "AttendFamilySearchFormViewController.h"
#import "AddNewPersonViewController.h"

@implementation AttendSearchResultsViewController

#define CheckInViewController [[self.navigationController viewControllers] objectAtIndex:1]

@synthesize table;
@synthesize fullname;

- (void)viewDidLoad {
	[super viewDidLoad];
	
	UIBarButtonItem* searchButton = [[UIBarButtonItem alloc] initWithTitle:@"Family Search" style:UIBarButtonItemStylePlain target:self action:@selector(searchByFamily:)];
	self.navigationItem.rightBarButtonItem = searchButton;
	[searchButton release];
	
}

/*
 Triggered when the user selects a table row.
 */
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {	
	if( indexPath.row < 20 ) {
		NSUInteger row = [indexPath row];
		NSDictionary *person = [self.resultsData objectAtIndex:row];
		NSLog(@"adding peopleId: %@", [person valueForKey:@"peopleid"]);
		
		//[[NSNotificationCenter defaultCenter] postNotificationName:@"RecordVisit" object:[person valueForKey:@"peopleid"]];
		[CheckInViewController recordVisit:[person valueForKey:@"peopleid"]];
		[self.navigationController popToViewController:CheckInViewController animated:YES]; 	
	}
}


- (void)searchByFamily:(id)sender {
	NSLog(@"search by family");
	AttendFamilySearchFormViewController *familySearchView = [[AttendFamilySearchFormViewController alloc]
												 initWithNibName:@"AttendFamilySearchFormView" bundle:[NSBundle mainBundle]];
	NSLog(@" full name = %@", self.fullname);
	if(fullname) {
		NSArray *words = [fullname componentsSeparatedByString:@" "];
		if([words count] > 0) {
			familySearchView.populateName = [words objectAtIndex:([words count]-1)];
		}
	}
	[self.navigationController pushViewController:familySearchView animated:YES];
	[familySearchView release];
	
}


- (void)dealloc {
	[table release];
	[fullname release];
    [super dealloc];
}


@end
